package com.jy.servlet;

import com.alibaba.fastjson.JSON;
import com.jy.bean.User;
import com.jy.service.ShoppingTrolleyService;
import com.jy.service.UserService;
import com.jy.service.impl.ShoppingTrolleySericelmpl;
import com.jy.service.impl.UserServicelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class cart_shoppin_empty_Servlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(cartServlet.class);
    private ShoppingTrolleyService shoppingTrolleyServlet;
    private UserService userService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_name = request.getParameter("user_name");

        shoppingTrolleyServlet = new ShoppingTrolleySericelmpl();
        userService = new UserServicelmpl();

        List<User> users = userService.selectByname(user_name);

        boolean favg = false;
        for (User user:users){
            favg = shoppingTrolleyServlet.deleteById(user.getId());
        }

        //查询该账号的购物车数量，并给到session里面去
        HttpSession session = request.getSession();
        int Shopping_count = shoppingTrolleyServlet.selectByName(user_name);
        session.setAttribute("Shopping_count",Shopping_count);

        String data = JSON.toJSONString(favg);

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(data);
        log.debug("data:" +data);
    }
}
